package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.exartisan.adapter.AdapterDetailNormal;
import com.exartisan.bean.FbComment;
import com.exartisan.network.FacebookResponseParser;
import com.exartisan.network.FbConnnector;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.NewParamBuilder;
import com.exartisan.network.NewRestConnector;
import com.exartisan.util.DataHolder;
import com.exartisan.util.FbUtil;
import com.exartisan.util.OnCancelAuthenticate;
import com.exartisan.util.Util;
import com.facebook.Session;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class DetailNormalActivity extends ListActivity {
	
	private AdapterDetailNormal adapter;
	private RelativeLayout btnSend;
	private EditText edtComment;
	private boolean liked;
	private ProgressBar prBarSend;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_detail_normal);
		
		btnSend = (RelativeLayout) findViewById(R.id.layout_send);
		edtComment = (EditText) findViewById(R.id.edtComment);
		prBarSend = (ProgressBar) findViewById(R.id.progressBarSend);
		
		btnSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (edtComment.length() < 1){
						Toast.makeText(getBaseContext(), "Bạn chưa nhập bình luận", Toast.LENGTH_SHORT).show();
						return;
				}
				prBarSend.setVisibility(View.VISIBLE);
				btnSend.setVisibility(View.INVISIBLE);
				FbUtil.commentOnId(edtComment.getText().toString(), DataHolder.getPost().getFbFeedID(), getActivity(), new AsyncHttpResponseHandler(){
					
					@Override
					public void onSuccess(int arg0, String response) {
						prBarSend.setVisibility(View.GONE);
						btnSend.setVisibility(View.VISIBLE);
						Log.d("commentResponse", response.toString());
						edtComment.setText("");
						Util.hideSoftKeyboard(getActivity());
						getFbComments();
						super.onSuccess(arg0, response);
					}
					
					@Override
					public void onFailure(Throwable arg0, String arg1) {
						prBarSend.setVisibility(View.GONE);
						btnSend.setVisibility(View.VISIBLE);
						Toast.makeText(getActivity(), "Không thể đăng bình luận", Toast.LENGTH_SHORT).show();
						Log.d("commentResponse", "fail");
						super.onFailure(arg0, arg1);
					}
					
				}, new OnCancelAuthenticate() {
					
					@Override
					public void CancelAuthent() {
						prBarSend.setVisibility(View.GONE);
						btnSend.setVisibility(View.VISIBLE);
					}
				});
				
				
			}
		});
		
		//Post post = new Post(1, 1, 1, "abcdef", "wtf", "22/09/2013", "vai nham", "haivl", "http://tai-facebook.net/wp-content/uploads/2013/02/avatar-facebook.jpg", "http://tai-facebook.net/wp-content/uploads/2013/02/avatar-facebook.jpg");
		ArrayList<FbComment> fbComments = new ArrayList<FbComment>();
		/*for (int i = 0; i < 10; i++) {
			FbUser fbUser = new FbUser();
			fbUser.setId("123");
			fbUser.setName("Haivl");
			FbComment fbComment = new FbComment("1", "hello world!", "22/09/2013", fbUser, 3);
			fbComments.add(fbComment);
		}*/
		adapter=new AdapterDetailNormal(this, DataHolder.getPost(), fbComments, true, NetworkUtility.POST_WEB_VIEW_URL + DataHolder.getPost().getPostID());
		setListAdapter(adapter);
		getFbComments();
		NewRestConnector.post("", NewParamBuilder.BuildParams(NewParamBuilder.BuildDetailPostData(DataHolder.getPost().getPostID())), new AsyncHttpResponseHandler());
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode,
				resultCode, data);

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		FbUtil.logout(getBaseContext());
		return super.onOptionsItemSelected(item);
	}
	
	private void getFbComments() {
		if (NetworkUtility.checkNetworkState(getApplicationContext())) {
			// prBar.setVisibility(View.VISIBLE);
			// Log.d("feedID", event.getFbFeedID());
			// RequestParams params = new RequestParams("ids",
			// "453407711441834_458009987648273");
			FbConnnector.get(DataHolder.getPost().getFbFeedID(),
					NetworkUtility.CONNECTION_COMMENT, new RequestParams("access_token", FbUtil.ACCESS_TOKEN),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int arg0, String response) {
							// prBar.setVisibility(View.GONE);
							if (response == null) {
								Toast.makeText(getApplicationContext(),
										"Không thể tải bình luận",
										Toast.LENGTH_SHORT).show();
								return;
							}
							super.onSuccess(arg0, response);
							Log.d("fbRes", response.toString());
							ArrayList<FbComment> comments = FacebookResponseParser
									.getComments(response);
							Log.d("comments_num", comments.size() + "");
							adapter.setComments(comments);
							adapter.notifyDataSetChanged();
						}

						@Override
						public void onFailure(Throwable arg0, String arg1) {
							// prBar.setVisibility(View.GONE);
							Toast.makeText(getApplicationContext(),
									"Không thể tải bình luận",
									Toast.LENGTH_SHORT).show();
							super.onFailure(arg0, arg1);
						}

					});
		} else
			Toast.makeText(this, "Không có kết nối", Toast.LENGTH_SHORT).show();
	}
	
	
	public void onClickGalleryBlog(View v){
		Intent intent=new Intent(this,GalleryActivity.class);
		startActivity(intent);
	}
	
	public void onClickBack(View v){
		finish();
	}
	
	public void onClickCover(View v){
		
	}
	
	public void onClickLike(View v){
		final ImageButton btnLike;
		final TextView tvLike;
		
		btnLike=(ImageButton) getActivity().findViewById(R.id.btnLikeBlog);
		tvLike=(TextView) getActivity().findViewById(R.id.tvLikesBlog);
		//btnLike.
		if(!liked){
		FbUtil.likeId(DataHolder.getPost().getFbFeedID(), getActivity(), new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(int arg0, String response) {
				liked = true;
				btnLike.setBackgroundResource(R.drawable.like_red);
				tvLike.setTextColor(Color.RED);
				Util.setLikeCount(DataHolder.getPost().getFbFeedID(), tvLike);
				super.onSuccess(arg0, response);
				
			}
		});
		} else {
			
			FbUtil.unLikeId(DataHolder.getPost().getFbFeedID(), getActivity(), new AsyncHttpResponseHandler(){
				@Override
				public void onSuccess(int arg0, String response) {
					liked = false;
					btnLike.setBackgroundResource(R.drawable.like_black);
					tvLike.setTextColor(Color.BLACK);
					Util.setLikeCount(DataHolder.getPost().getFbFeedID(), tvLike);
					super.onSuccess(arg0, response);
					
				}
			});
		}
	}
	
	public void onClickComment(View v){
		((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
		edtComment.requestFocus();
	}

	
	private Activity getActivity(){
		return this;
	}
}
